<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>

    <title>Hello React Lite Project</title>

</head>
<!--<script defer src="./dist/bundle.js"></script>-->
<body>
<div id="rootdiv"></div>
this is a test

<div id="litedemo"></div>

<div id="component1"></div>
<div id="component2"></div>
<div id="bcosdemo"></div>

<script src="./pub/jquery-3.6.4.min.js"></script>
<script src="./pub/utils/bcosutils.js"></script>
<script src="./pub/utils/net_utils.js"></script>

<hr/>
<p>0) blocklimit : <span id="blocklimit" style="color: blue; ">0</span></p>
<p>1) ABI:<span id="abi" style="color: blue; "></span></p>
<p>2) FunctionData: <span id="functiondata" style="color: #00AB22; "></span></p>
<p>3) FunctionData: <span id="getdata" style="color: #00AB22; "></span></p>

<button onclick="doGet(getblocklimit_url,on_block_limit)" >[start]</button>
<p>3)<span style="color: #FF6622; "><B>-->生成的raw transaction的完整hex串：</B>
    (字节数<span id="rawtxhexlen" style="color: #0066FF; "/>0 </span>)</p>
<p><textarea id="rawtxhextext" style="width: 100%;" rows="10"></textarea></p>

<hr/>
<p><span id="debug" style="color: #006600; ">Debug.....</span></p>

<script type="module">
    import HelloWorldContract from "./pub/HelloWorld.js";
    var contract = new HelloWorldContract();
    var function_data = contract.create_set_data("ahtmltest---12");
    var get_data = contract.create_get();
    document.getElementById("functiondata").innerText =  function_data;
    document.getElementById("abi").innerText =  contract.abiStr;
    document.getElementById("getdata").innerText = contract.create_get()
</script>
<script>
    function debug(txt) {
        document.getElementById("debug").innerText += "\n" + txt;
    }
    var privkey = "255f01b066a90853a0aa18565653d7b944cd2048c03613a9ff31cb9df9e693e5";
    var toAddress = "0xbe6e9bef148d9c3ac77f28c426e609ea7b7e437a";

    function make_tx_data(blocklimit) {
        var function_data = document.getElementById("functiondata").innerText;

        var txData = new bcosutils.bcostars.TransactionData();
        txData.version = 1;
        txData.chainID = "chain0";
        txData.groupID = "group0";
        //alert("function_data:"+function_data)
        bcosutils.TxUtils.set_txdata_input(txData, function_data);
        var input = bcosutils.DataUtils.arrayToHex(bcosutils.DataUtils.bufferToBytes(txData.input));
        //console.log("input",input);
        txData.to = toAddress;
        txData.blockLimit = blocklimit; //需要从节点查询，动态更新
        txData.nonce = bcosutils.DataUtils.bigIntRandom().toString(); //nonce是一个整形数的字符串
        return txData
    }
    function sign_raw_tx(txData) {
        // start to sign & encode txraw
        var rawtxbin = bcosutils.TxUtils.build_rawtransaction_by_txdata(txData, privkey);
        var rawtxhex = bcosutils.DataUtils.arrayToHex(rawtxbin);
        return rawtxhex;
    }

    function build_raw_tx(blocklimit){
        var txData = make_tx_data(blocklimit);
        var rawtxhex = sign_raw_tx(txData);
        return rawtxhex;
    }
       // var rawtxhex = build_raw_tx(blocklimit)
       // document.getElementById("rawtxhextext").innerText = rawtxhex;
       // document.getElementById("rawtxhexlen").innerText = rawtxhex.length;

    function fire_raw_tx(blocklimit) {
        var rawtxhex = build_raw_tx(blocklimit)
        document.getElementById("rawtxhextext").innerText = rawtxhex;
        document.getElementById("rawtxhexlen").innerText = rawtxhex.length;
        var abiStr =  document.getElementById("abi").innerText;
        debug("privkey:" + privkey);
        debug("rawhex len:" + rawtxhex.length);
        debug(rawtxhex);

        var url= "http://localhost:8000/raw/";
        var data  ={"cmd":"sendtx","address":toAddress,rawdata:rawtxhex,"abi":abiStr}
        doPost(url,data,on_response)
    }

    const getblocklimit_url = "http://localhost:8000/rpc/?cmd=getBlocklimit";
    function on_block_limit(data,status)
    {
        console.log("on_block_limit:"+data);
        var datatext = JSON.stringify(data);
        $("#blocklimit").text(data)
        fire_raw_tx(data)
    }
    //doGet(getblocklimit_url,on_block_limit);

    var url= "http://localhost:8000/raw/";
    function on_get(data,status)
    {
        console.log("get result "+data)
        //alert(data);
        debug("getResult : "+data)
    }
    function on_response(data,status){
        var get_data = document.getElementById("getdata").innerText;
        var abiStr =  document.getElementById("abi").innerText;
        console.log("on_response",data);
        var data  ={"cmd":"call","address":toAddress,rawdata:get_data,"abi":abiStr}
        doPost(url,data,on_get)
    }

    //setInterval("doGet(getblocklimit_url,on_block_limit)",2000);
</script>

</body>
</html>